import AudioManager from "./AudioManager";
import PeripheralSystemDataMgr from "./PeripheralSystemDataMgr";
import MathUtil from "./MathUtil";
import BasePageCtrl, { AnimType } from "./BasePageCtrl";
import PeripheralSystemMgr from "./PeripheralSystemMgr";
import UiManage from "./UiManage";
import WithDrawProportionIncreasePage from "./WithDrawProportionIncreasePage";
import MathUtils from "./MathUtil";

const { ccclass, property } = cc._decorator;

@ccclass
export default class WithDrawProportionIncreasePageCtrl extends BasePageCtrl {
    @property(WithDrawProportionIncreasePage)
    ui = null;

    onLoad() {
        super.onLoad();
        this.onUILoad();
        this._animType = AnimType.SCALE;
        this._touchControl = false;
        this._hasPeneLock = true;
        this._hasBlack = true;
        this._hasTouchLock = false;
        this._hasBlackTouch = false;
        this.addButtonListen();
    }

    start() {}

    onUILoad() {
        this.ui = this.node.addComponent(WithDrawProportionIncreasePage);
    }

    _init() {
        const t = PeripheralSystemDataMgr.getTravleData();
        const a = t.cash_before;
        const o = t.cash_after;
        const i = MathUtils.accMul(a, 100);
        const currentProportion = this.fomatFloat(i, 2);
        const r = MathUtils.accMul(o, 100);
        const increastProportion = this.fomatFloat(r, 2);
        this.ui.currentProportionLabel.getComponent(cc.Label).string = `${currentProportion}%`;
        this.ui.increastProportionLabel.getComponent(cc.Label).string = `${increastProportion}%`;
        this.ui.light.active = false;
        const c = [
            this.ui.currentProportionNode,
            this.ui.arrow,
            this.ui.increastProportionNode,
            this.ui.btn_withdraw
        ];
        c.forEach((e, t) => {
            e.opacity = 0;
        });
        AudioManager.getInstance().playMusic("red_increase");
        for (let t = 0; t < c.length; t++) {
            const a = c[t];
            const o = 0.3 * t;
            cc.tween(a)
                .delay(o)
                .to(1, { opacity: 255 })
                .call(() => {
                    if (t === 2) {
                        this.ui.light.active = true;
                        AudioManager.getInstance().playMusic("increase");
                    }
                })
                .start();
        }
    }

    fomatFloat(e, t) {
        return Math.round(e * Math.pow(10, t)) / Math.pow(10, t);
    }

    addButtonListen() {
        UiManage.addButtonListen(
            this.ui.btn_withdraw,
            this.clickWithDraw,
            this
        );
    }

    clickWithDraw() {
        this.clickClose();
        PeripheralSystemMgr.getExtractInfo(true);
    }

    clickClose() {
        this.hide();
    }

    static prefabUrl = "WithDrawProportionIncreasePage";
    static className = "WithDrawProportionIncreasePageCtrl";

}

